﻿package com.trs.publish.util.common;
import java.security.*;
/**
 * 加密
 *
 */
public class Coder {

	/**
	 * 加密
	 * @param string 明文
	 * @return 密文
	 */
	public static String code(String string) {
		try {
			string=byte2hex(md5(sha(sha(string.getBytes()))));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return string;
	}

	public static byte[] md5(byte[] obj) {
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(obj);
			return md5.digest();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return obj;
	}
	
	public static byte[] sha(byte[] obj) {
		try {
			MessageDigest sha = MessageDigest.getInstance("SHA");
			sha.update(obj);
			return sha.digest();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return obj;
	}
	
	public static String byte2hex(byte[] b) // 二行制转字符串
	{
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
			if (n < b.length - 1)
				hs = hs + "";
		}
		return hs.toUpperCase();
	}
	
	public static void main(String[] args){
		Coder coder = new Coder();
		String before = "abcde@12345";
		System.out.println(before);
		String after = coder.code(before);
		System.out.println(after);
	}

}